package com.itheima.thread.forkjoinTest;

import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;

/**
 * @Author: Ethan Yankang
 * @Program: Interview
 * @Date: 2024-08-18 15:46
 **/
@Slf4j
public class ForkJoinTest extends RecursiveTask<Integer> {
    public static final int threshold = 1;
    private int start;
    private int end;
    public ForkJoinTest(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Integer compute() {
        int sum = 0;
        //如果任务足够小就计算任务
        boolean canCompute = (end - start) <= threshold;
        if (canCompute) {
            for (int i = start; i <= end; i++) {
                sum += i;
            }
        } else {
            // 如果任务大于阈值，就分裂成两个子任务计算
            int middle = (start + end) / 2;
            ForkJoinTest leftTask = new ForkJoinTest(start, middle);
            ForkJoinTest rightTask = new ForkJoinTest(middle + 1, end);

            // 执行子任务
            leftTask.fork();
            rightTask.fork();

            // 等待任务执行结束合并其结果
            int leftResult = leftTask.join();
            int rightResult = rightTask.join();

            // 合并子任务
            sum = leftResult + rightResult;
        }
        return sum;
    }

    public static void main(String[] args) {
        ForkJoinPool forkjoinPool = new ForkJoinPool();

        Instant start = Instant.now();
        long time1=System.nanoTime();
        //生成一个计算任务，计算1+2+3+4
        ForkJoinTest task = new ForkJoinTest(1, 10000000);

        //执行一个任务

        Future<Integer> result = forkjoinPool.submit(task);
        Instant end = Instant.now();
        // 计算时间差
        Duration duration = Duration.between(start, end);
        long time2=System.nanoTime();
        System.out.println("Duration = " + duration.toMillis() + " milliseconds");
        System.out.println("Duration = " + (time2-time1) + " nonaSeconds");

        try {
            log.info("result:{}", result.get());
        } catch (Exception e) {
            log.error("exception", e);
        }
    }
}
